package com.xbh.boot_study.handler;

import com.xbh.boot_study.exception.ApiException;
import com.xbh.boot_study.exception.ServiceException;
import com.xbh.boot_study.util.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.NotReadablePropertyException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;

/**
 * @ExceptionHandler: 异常处理器
 * @author: ZBoHang
 * @time: 2021/9/28 16:24
 */
@Slf4j
@RestControllerAdvice
public class ExceptionsHandler {

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(ServiceException.class)
    public ResponseData<String> handleRRException(ServiceException e) {
        log.error(e.getMsg(), e);
        return ResponseData.failure(e.getMsg());
    }

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(ApiException.class)
    public ResponseData<String> handleRRException(ApiException e) {
        log.error(e.getMsg(), e);
        return ResponseData.failure(e.getMsg());
    }

    /**
     * MethodArgumentNotValidException
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseData<String> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {

        return ResponseData.failure(e.getBindingResult().getFieldError().getDefaultMessage());
    }

    /**
     * ConstraintViolationException
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public ResponseData<String> handleConstraintViolationException(ConstraintViolationException e) {

        return ResponseData.failure(e.getConstraintViolations().stream().findFirst().get().getMessage());
    }

    /**
     * BindException
     */
    @ExceptionHandler(BindException.class)
    public ResponseData<String> handleBindException(BindException e) {

        return ResponseData.failure(e.getAllErrors().get(0).getDefaultMessage());
    }

    /**
     * NotReadablePropertyException
     */
    @ExceptionHandler(NotReadablePropertyException.class)
    public ResponseData<String> handleNotReadablePropertyException(NotReadablePropertyException e) {

        return ResponseData.failure("fail！");
    }

    /**
     * ValidationException
     */
    @ExceptionHandler(ValidationException.class)
    public ResponseData<String> handleValidationException(ValidationException e) {
        log.error(e.getCause().getMessage(), e);
        return ResponseData.failure(e.getCause().getMessage());
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public ResponseData<String> handlerNoFoundException(Exception e) {
        log.error(e.getMessage(), e);
        return ResponseData.failure("路径不存在，请检查路径是否正确");
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public ResponseData<String> handleDuplicateKeyException(DuplicateKeyException e) {
        log.error(e.getMessage(), e);
        return ResponseData.failure("数据重复，请检查后提交");
    }


    @ExceptionHandler(Exception.class)
    public ResponseData<String> handleException(Exception e) {
        log.error(e.getMessage(), e);
        return ResponseData.failure("系统繁忙,请稍后再试");
    }
}
